# -*- coding: utf-8 -*-

import json
from collections import defaultdict

with open("g3TopoTable.json", "r", encoding="utf-8") as f:
    data = json.load(f)

print(type(data))
print(data)

groups = defaultdict(list)
for node in data['g3topotable']:
    phase = node.get('phase')
    if phase in {'A', 'B', 'C', 'N'}:          # 只关心 A、B 两相
        eui64_no_colon = node['eui64'].replace(':', '')
        groups[phase].append(eui64_no_colon)

with open('g3_phase.txt', 'w', encoding='utf-8') as f:
    # A 相
    f.write('A相eui\n')
    f.write('\n'.join(groups['A']))
    f.write('\n\n')                  # 空一行分隔

    # B 相
    f.write('B相eui\n')
    f.write('\n'.join(groups['B']))
    f.write('\n\n')

    # C 相
    f.write('C相eui\n')
    f.write('\n'.join(groups['C']))
    f.write('\n\n')

    # N 相
    f.write('N相eui\n')
    f.write('\n'.join(groups['N']))
    f.write('\n\n')

print('已写入 g3_phase.txt')